package com.molyfun.parents.activity.myorder.view;

import android.content.Context;
import android.os.Handler;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.R;

import butterknife.ButterKnife;
import butterknife.InjectView;

import static com.molyfun.parents.R.id.order_all;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:10/18/16.
 */
public class OrderTopBar extends LinearLayout implements View.OnClickListener{

    private int mChooseTab = OrderBarChooseTab.BAR_ORDER_ALL;

    public static interface OrderBarChooseTab{
        int BAR_ORDER_ALL = 1001;
        int BAR_ORDER_TO_PAY = 1002;
        int BAR_ORDER_PAYD = 1003;
    }

    protected LayoutInflater layoutInflater;

    private Handler handler = new Handler();
    private OrderBarClickListener listener;
    private Context context;
    private boolean inited;

    @InjectView(order_all)
    TextView mOrderAll;
    @InjectView(R.id.order_to_pay)
    TextView mOrderToPay;
    @InjectView(R.id.order_paid)
    TextView mOrderPaid;


    public OrderTopBar(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public OrderTopBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public OrderTopBar(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.widget_my_order_top_choose_bar, this);
        ButterKnife.inject(this);
        mOrderAll.setOnClickListener(this);
        mOrderToPay.setOnClickListener(this);
        mOrderPaid.setOnClickListener(this);
    }

    public int getCurrentChooseTab(){
        return mChooseTab;
    }

    /**
     * init view
     *
     */
    public void init() {
        if(inited){
            return;
        }

        inited = true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case order_all:
                if(mChooseTab != OrderBarChooseTab.BAR_ORDER_ALL){
                    if(listener != null){
                        listener.onTopBarClick(OrderBarChooseTab.BAR_ORDER_ALL);
                    }
                    setTabClick(OrderBarChooseTab.BAR_ORDER_ALL);
                }
                break;
            case R.id.order_to_pay:
                if(mChooseTab != OrderBarChooseTab.BAR_ORDER_TO_PAY){
                    if(listener != null){
                        listener.onTopBarClick(OrderBarChooseTab.BAR_ORDER_TO_PAY);
                    }
                    setTabClick(OrderBarChooseTab.BAR_ORDER_TO_PAY);
                }
                break;
            case R.id.order_paid:
                if(mChooseTab != OrderBarChooseTab.BAR_ORDER_PAYD){
                    if(listener != null){
                        listener.onTopBarClick(OrderBarChooseTab.BAR_ORDER_PAYD);
                    }
                    setTabClick(OrderBarChooseTab.BAR_ORDER_PAYD);
                }
                break;
        }
    }

    /**
     * only view
     * @param tab
     */
    public void setTabClick(int tab){
        if(tab == OrderBarChooseTab.BAR_ORDER_ALL){
            mOrderAll.setBackgroundResource(R.drawable.bg_left_rounded_red);
            mOrderAll.setTextColor(ContextCompat.getColor(context, R.color.color_white));
            mOrderToPay.setBackgroundResource(R.drawable.bg_top_bottom_red);
            mOrderToPay.setTextColor(ContextCompat.getColor(context, R.color.color_base_title_bar));
            mOrderPaid.setBackgroundResource(R.drawable.bg_right_rounded_red_hollow);
            mOrderPaid.setTextColor(ContextCompat.getColor(context, R.color.color_base_title_bar));
            mChooseTab = OrderBarChooseTab.BAR_ORDER_ALL;
        }else if(tab == OrderBarChooseTab.BAR_ORDER_TO_PAY){
            mOrderAll.setBackgroundResource(R.drawable.bg_left_rounded_red_hollow);
            mOrderAll.setTextColor(ContextCompat.getColor(context, R.color.color_base_title_bar));
            mOrderToPay.setBackgroundColor(ContextCompat.getColor(context, R.color.color_base_title_bar));
            mOrderToPay.setTextColor(ContextCompat.getColor(context, R.color.color_white));
            mOrderPaid.setBackgroundResource(R.drawable.bg_right_rounded_red_hollow);
            mOrderPaid.setTextColor(ContextCompat.getColor(context, R.color.color_base_title_bar));
            mChooseTab = OrderBarChooseTab.BAR_ORDER_TO_PAY;
        }else{
            mOrderAll.setBackgroundResource(R.drawable.bg_left_rounded_red_hollow);
            mOrderAll.setTextColor(ContextCompat.getColor(context, R.color.color_base_title_bar));
            mOrderToPay.setBackgroundResource(R.drawable.bg_top_bottom_red);
            mOrderToPay.setTextColor(ContextCompat.getColor(context, R.color.color_base_title_bar));
            mOrderPaid.setBackgroundResource(R.drawable.bg_right_rounded_red);
            mOrderPaid.setTextColor(ContextCompat.getColor(context, R.color.color_white));
            mChooseTab = OrderBarChooseTab.BAR_ORDER_PAYD;
        }
    }

    public void setOrderTopBarClickListener(OrderBarClickListener listener) {
        this.listener = listener;
    }

    public interface OrderBarClickListener {
        void onTopBarClick(int chooseTab);
    }

}